import { _decorator, Component, Node, UITransform, view, Size } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('DisObjUtil')
export class DisObjUtil extends Component {
    /**
     * 
     * @param node 
     */
    public static toStageCenter(node:Node):void{
        if(DisObjUtil.isValid(node)){
            var transform:UITransform = node.getComponent(UITransform);
            if(null != transform){
                var screenSz:Size = view.getVisibleSize();//屏幕尺寸
                node.setPosition((screenSz.width - transform.width)/2 - node.position.x, (screenSz.height - transform.height)/2 - node.position.y);
                console.log("[DisObjUtil.toStageCenter] screenSz.width = " + screenSz.width + " screenSz.height = " + screenSz.height);
            }
        }
    }

    public static toParent0Point(node:Node):void{
        if(DisObjUtil.isValid(node)){
            var parentNode:Node = node.parent;
            var transform:UITransform = node.getComponent(UITransform);
            var ptransform:UITransform = parentNode.getComponent(UITransform);
            if(null != transform && null != ptransform){
                var px:number = node.position.x - (parentNode.position.x + ptransform.width/2);
                var py:number = node.position.y - (parentNode.position.y + ptransform.height/2);
                node.setPosition(px, py);
            }
        }
    }

    public static toParentCenter(node:Node):void{
        if(DisObjUtil.isValid(node)){
            var transform:UITransform = node.getComponent(UITransform);
            if(null != transform){
                var parentNode:Node = node.parent;
                var ptransform:UITransform = parentNode.getComponent(UITransform);
                if(null != ptransform){
                    var px:number = parentNode.position.x + ptransform.width/2 - (node.position.x + transform.width/2);
                    var py:number = parentNode.position.y + ptransform.height/2 - (node.position.y + transform.height/2);
                    node.setPosition(px, py);
                }
            }
        }
    }

    public static getChildByType(typeClass:any, container:Node):Node{
        if(DisObjUtil.isValid(container)){
            for(var i=0;i<container.children.length;i++){
                if(typeof(container.children[i]) == typeClass){
                    return container.children[i];
                }
            }
        }
        return null;
    }

    public static removeMe(node:Node):void{
        if(DisObjUtil.isValid(node)){
            node.destroy();
        }
    }

    public static toTop(node:Node):void{
        if(DisObjUtil.isValid(node)){
            node.parent.setSiblingIndex(node.parent.children.length - 1);
        }
    }

    /**
     * 判断节点是否有效
     * @param node 
     * @returns 
     */
    public static isValid(node:Node){
        if(null != node && null != node.parent && node.isValid){
            return true;
        }else{
            return false;
        }
    }
    
    public static removeAllChildren(node:Node):void{
        if(DisObjUtil.isValid(node)){
            node.removeAllChildren();
        }
    }
}

